package org.gruposp2p.dnie.client.ui.panel;

import org.gruposp2p.dnie.client.DNIeConstants;
import org.gruposp2p.dnie.client.ui.dialogs.AccesingPrivateZoneDialog;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.allen_sauer.gwt.log.client.Log;

public class MainPanel extends Composite 
	implements EntryPoint, ValueChangeHandler<String>{

    private static MainPanelUiBinder uiBinder = GWT
			.create(MainPanelUiBinder.class);

    interface MainPanelUiBinder extends UiBinder<ScrollPanel, MainPanel> { }

    public static MainPanel INSTANCE;
    public static String HISTORY_UPLOAD_AUTHENTICATED = "#uploadDocumentAuthenticated";
    public static String HISTORY_LOGIN_AUTHENTICATED = "#loginAuthenticated";

    @UiField ScrollPanel mainScrollPanel;
    
    @UiField VerticalPanel mainPanel;
    @UiField TopPanel topPanel;
    @UiField HeaderPanel headerPanel;
    public @UiField PublicCentralPanel publicCentralPanel;
    public @UiField PrivateCentralPanel privateCentralPanel;
    @UiField BottomPanel bottomPanel;
    @UiField VerticalPanel selectedMainPanel;
    
    AccesingPrivateZoneDialog dialog;
    
    public boolean loading = true;
    public boolean authenticated = false;

    public static DNIeConstants constants =
            (DNIeConstants) GWT.create(DNIeConstants.class);

    public MainPanel() {
            INSTANCE = this;
    }

    public void onModuleLoad() {
    	ScrollPanel panel = uiBinder.createAndBindUi(this);
        RootLayoutPanel root = RootLayoutPanel.get();
        RootPanel rootPanel =RootPanel.get("body");
        if (rootPanel != null) {
        	//Explorer y Chrome
        	rootPanel.add(panel);
        } else {
        	//Firefox
        	root.add(panel);
        }
        History.addValueChangeHandler(this);
        String protocol = com.google.gwt.user.client.Window.Location.getProtocol();
        if ("https".equals(protocol)) {
            setSelectedPanel(privateCentralPanel);
        } else {
            setSelectedPanel(publicCentralPanel);
        }
        History.fireCurrentHistoryState(); 
        loading = false;
    }
    
    public void setSelectedPanel(Composite selectedPanel) {
    	if (selectedPanel == null) return;
		if (selectedMainPanel != null) {
			selectedMainPanel.clear();
			selectedMainPanel.setHeight("400px");
    		if (selectedPanel.equals(publicCentralPanel)) {
    			selectedMainPanel.add(publicCentralPanel);
    			topPanel.setNavigationMode(TopPanel.HISTORY_LOGOUT);
    		} else if (selectedPanel.equals(privateCentralPanel)) {
    			selectedMainPanel.add(privateCentralPanel);
    			topPanel.setNavigationMode(TopPanel.HISTORY_UPLOAD_DOCUMENT);
    		}
		}
    }

	@Override
	public void onValueChange(ValueChangeEvent<String> event) {
		String historyToken = event.getValue();
		if (TopPanel.HISTORY_UPLOAD_DOCUMENT.equals(historyToken)) {
			if (!authenticated) {
				dialog = new AccesingPrivateZoneDialog();
				dialog.show();
			} else {
				setSelectedPanel(privateCentralPanel);
			}
		} else if (TopPanel.HISTORY_LOGIN.equals(historyToken)) {
			if (!authenticated) {
				dialog = new AccesingPrivateZoneDialog();
				dialog.show();
			} else {
				setSelectedPanel(privateCentralPanel);
			}
		} else if (TopPanel.HISTORY_LOGOUT.equals(historyToken)) {
			setSelectedPanel(publicCentralPanel);
			//goToPublicZone();
		} else if (HISTORY_UPLOAD_AUTHENTICATED.equals(historyToken)) {
            authenticated = true;
			History.newItem(TopPanel.HISTORY_UPLOAD_DOCUMENT);
		} else if (HISTORY_LOGIN_AUTHENTICATED.equals(historyToken)) {
            authenticated = true;
			History.newItem(TopPanel.HISTORY_LOGIN);
		}
	}

	public native void goToPublicZone() /*-{ 
    	$wnd.location.reload(URLConstants.getPublicUrl()); 
	}-*/; 
	
}